#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>

static char *opt_host_name = "10.1.1.44";
static char *opt_user_name = "root";
static char *opt_password = "ta0mee@321";
static unsigned int opt_port_num = 0;	//零表示使用默认端口号
static char *opt_socket_name = NULL;	//NULL表示使用默认套接字	
static char *opt_db_name = "maggie";	//database name
static unsigned int opt_flags = 0;

static MYSQL *conn;
static int FLAG = 1;

static void print_error(MYSQL *conn, char *message)
{
	fprintf(stderr, "%s\n", message);
	if(conn != NULL)
	{
		fprintf(stderr, "Error %u (%s): %s\n",
			mysql_errno(conn), mysql_sqlstate(conn), mysql_error(conn));
	}
}

void process_result_set(MYSQL *conn, MYSQL_RES *res_set)
{
	MYSQL_ROW row;
	unsigned int i;
	
	while((row = mysql_fetch_row(res_set)) != NULL){
		for(i = 0; i < mysql_num_fields(res_set); i++){
			if (i > 0)
				fputc ('\t', stdout);
			printf("%s", row[i] != NULL ? row[i] : "NULL");
		}
		fputc ('\n', stdout);
	}
	if (mysql_errno(conn) != 0)
		print_error(conn, "mysql_fech_row() failed\n");
	else
		printf("Number of rows retured: %lu\n",
			(unsigned long) mysql_num_rows(res_set));
}

void process_statement(MYSQL *conn, char *stmt_str)
{
	MYSQL_RES *res_set;
	FLAG = 1;
	
	if (mysql_query (conn, stmt_str) != 0){
		print_error(conn, "could not execute statement");
		FLAG = 0;
		return;		
	}

	res_set = mysql_store_result(conn);
	if (res_set){
		process_result_set(conn, res_set);
		mysql_free_result(res_set);
	} else {
		if(mysql_field_count(conn) == 0){
			printf("Number of rows affect: %lu\n",
					(unsigned long) mysql_affected_rows(conn));
		} else {
			print_error(conn, "could not retrieve result set");
			FLAG = 0;
		}
	}
}

int main(int argc, char *argv[])
{

//	MY_INIT(argv[0]); 
	// initialize client library
	if (mysql_library_init (0, NULL, NULL))
	{
		fprintf (stderr, "mysql_library_int() failed\n");
		exit (1);
	}
	
	//initialize connection handler
	conn = mysql_init(NULL);
	
	if (conn == NULL)
	{
		print_error(conn, "mysql_real_connect() failed");
		exit (1);
	}
	
	//connect to server
	if (mysql_real_connect(conn, opt_host_name, opt_user_name, opt_password,
			opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)
	{
		
		print_error(conn, "mysql_real_connect() failed");
		mysql_close(conn);
		exit(1);
	}

	//insert into
/*
	if (mysql_query(conn,"INSERT INTO cats SET name = 'apple'") != 0){
		print_error(conn, "INSERT failed\n");
	} else {
		printf("INSERT statement succeed; number of rows affected: %lu\n",
			(unsigned long) mysql_affected_rows(conn));
	}
*/
	
	//SHOW TABLES;
	MYSQL_RES *res_set;
	MYSQL_ROW row;
/*
	if (mysql_query(conn, "SHOW TABLES") != 0)
		print_error(conn, "mysql_query() failed");
	else {
		res_set = mysql_store_result(conn);
		if (res_set == NULL)
			print_error (conn, "mysql_store_result() failed\n");
		else {
			process_result_set (conn, res_set);
			mysql_free_result(res_set);
		}
	}
*/

	if(mysql_autocommit(conn, 0) == 0) {
		process_statement(conn, "INSERT INTO cats(name) VALUES('mysql')");
		if(!FLAG) mysql_rollback(conn);
		process_statement(conn, "INSERT INTO cats(name) VALUES('apple')");
		if(!FLAG) mysql_rollback(conn);
		else	mysql_commit(conn);
	} else {
		print_error(conn, "set sutocommit = 0 failed");
	}

	process_statement(conn, "SELECT * FROM cats");

	//disconnect from server, terminate client library
	mysql_close(conn);
	mysql_library_end();

	return 0;
}
